var wcmp = require('../../../wechat').wcmp;

//接口菜单控制器
var menuController = {
	query: function (req, res, next) {
		wcmp.api.getMenu(function (err, result) {
			if (err) {
				if (err.code == 46003) {
					res.send('不存在菜单数据');
				}
				else {
					res.send(err);
				}
				return;
			}

			res.send(result);
		});
	},
	create: function (req, res, next) {
		//第二参数：开发者自行提供的数据
		var urlPrefix = "http://" + (process.env.wechat_host || '420400.dowei8.com') + "/mstores/"+(process.env.wechat_store || "headquarter");
		var authorizeURL = wcmp.oauth.getAuthorizeURL(urlPrefix + "/index.html#?sns=wechat", "oauthState", "snsapi_base");

		function toUrl(url) {
			var sep = '';
			if (url.indexOf('#') < 0)
				sep = '#'
			return wcmp.oauth.getAuthorizeURL(urlPrefix + url + sep + "?sns=wechat", "oauthState", "snsapi_base");
		}

		var test = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1ec49cee0d190d5c&redirect_uri=http%3A%2F%2Fevent.woshuo123.com%2Fafx517%2Fwechat%2FShareActivity%21bonusOauth2.action&response_type=code&scope=snsapi_base&state=oauthState&connect_redirect=1#wechat_redirect";

		//自定义菜单json
		var menu = {
			"button": [
				{
					"type": "click",
					"name": "商品",
					"key": "V1001_TODAY_MUSIC"
				},
				{
					"name": "会员",
					"sub_button": [
						{
							"type": "click",
							"name": "热门商品",
							"key": "V1001_HOT_GOODS"
						},
						{
							"type": "click",
							"name": "今日特价",
							"key": "V1001_TODAY_SPECAIL"
						},
						{
							"type": "view",
							"name": "我的会员卡",
							"url": toUrl("/index.html#/member-center")
						},
					]
				},
				{
					"name": "关于",
					"sub_button": [
						{
							"type": "view",
							"name": "孙记膳房",
							"url": toUrl("/index.html")
						}, {
							"type": "view",
							"name": "测试2",
							"url": test
						}]


				}]
		};

		wcmp.api.createMenu(menu, function (err, result) {
			if (err) {
				res.send(err);
				return;
			}
			res.send(result);
		});
	},
	remove: function (req, res, next) {
		wcmp.api.removeMenu(function (err, result) {
			if (err) {
				res.send(err);
				return;
			}
			res.send(result);
		});
	},
};

module.exports = menuController;